package cn.tedu.announcement.management.service.impl;

import cn.tedu.announcement.management.dao.persist.repository.IAnnouncementRepository;
import cn.tedu.announcement.management.pojo.entity.Notice;
import cn.tedu.announcement.management.pojo.param.NoticeParam;
import cn.tedu.announcement.management.pojo.vo.NoticeVO;
import cn.tedu.announcement.management.service.IAnnouncementService;
import cn.tedu.common.enumerator.ServiceCode;
import cn.tedu.common.ex.ServiceException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AnnouncementServiceImpl implements IAnnouncementService {
    @Autowired
    private IAnnouncementRepository announcementRepository;

    @Override
    public void addNew(NoticeParam noticeParam) {
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeParam,notice);
        int rows = announcementRepository.insert(notice);
        if (rows != 1) {
            String message = "发布公告失败，服务器忙，请稍后再试！";
            throw new ServiceException(ServiceCode.ERROR_INSERT, message);
        }
    }

    @Override
    public List<NoticeVO> listAll() {
        return announcementRepository.listAll();
    }

    @Override
    public void deleteById(Long id) {
        announcementRepository.deleteById(id);
    }
}
